package jveins.mto;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import jveins.VeinsAdapter;
import jveins.base.*;

public class MTOVeh extends BaseVeh {
	private int neighbourUpdate;
	private double MinObservationInterval;
	private double MaxObservationInterval;
	private boolean amSmart;
	public boolean isInit;
	
	public MTOVeh(VeinsAdapter initAdapter, int initID){
		super(initAdapter,initID);
		amSmart = true;
		isInit = false;
		System.out.println("Java: MTOVeh created with id=" + initID);
		
		Properties p = new Properties();
		try {
			p.load(new FileInputStream("config.properties"));
			neighbourUpdate = new Integer(p.getProperty("pNeighbourUpdate")).intValue();
			MinObservationInterval = new Double(p.getProperty("pMinObservationInterval")).doubleValue();
			MaxObservationInterval = new Double(p.getProperty("pMaxObservationInterval")).doubleValue();
			System.out.println("Neighbour Update Interval: " + neighbourUpdate);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void init(){
		scheduleNextObservationSend();
	}
	
	public void onSelfMsg(short kind){
		if(!isInit && kind == 1){
			init();
			isInit = false;
			System.out.println("Vehicle id#" + vehID + " has been initialized");
		}else{
			System.out.println("Vehicle id#" + vehID + " ignoring msg: " + kind);
		}
	}
	
	public void scheduleNextObservationSend(){
		if(!amSmart){
			return;
		}
		
		double delay = MinObservationInterval + (adapter.random().nextDouble() * (MaxObservationInterval-MinObservationInterval));
		log("Scheduling next observation send in " + delay);
		
		String msg = "sendobs/" + delay;

		System.out.println("Java: Scheduling next observation: " + msg);
		adapter.scheduleSelfMsg(vehID, msg);
		System.out.println("Sent that msg");
	}
}
